package ginutil

import (
	"gitee.com/xuyiping_admin/pkg/etagutil"
	"github.com/gin-gonic/gin"
)

// ShouldMatchWeakETag match weak etag
// return true if matched or
// set etag and return false if not match
func ShouldMatchWeakETag(c *gin.Context, keys ...string) bool {
	etag := etagutil.GenerateETag(true, keys...)
	if MatchETag(c, etag) {
		return true
	}

	RenderETag(c, etag)
	return false
}

func MatchETag(c *gin.Context, etag string) bool {
	return c.GetHeader("If-None-Match") == etag
}

func RenderETag(c *gin.Context, etag string) {
	c.Header("ETag", etag)
}
